import { reqGetSearchInfo } from '@/api'
//存放数据的仓库
const state = {
    //数据初始化
    //如果这里不把数据结构写完整，那么getter读取state.SearchInfo.data.goodsList时可能回报undefined的错误
    SearchInfo: {
        data:{
            goodsList:[]
        }
    }
}
//书写业务逻辑
const actions = {
    //获取search模块数据
    //函数上下文里面有一个commit对象，设置paramsm默认为空对象
    async getSearchList({ commit }, params = {}) {
        //params是dispatch时传过来的参数，至少是一个空对象
        let result = await reqGetSearchInfo(params)
        if (result.code == 200) {
            commit('SEARCHINFO', result.data)
        }
    }
}
//修改数据的唯一手段
const mutations = {
    SEARCHINFO(state, data) {
        state.SearchInfo = data
    }
}
//计算属性，为了简化数据而生
const getters = {
    //state是当前仓库的state
    goodsList(state) {
        return state.SearchInfo.goodsList || {}
    },
    trademarkList(state) {
        return state.SearchInfo.trademarkList
    },
    attrsList(state) {
        return state.SearchInfo.attrsList
    },
}

export default {
    state, mutations, actions, getters
}